
// UART 

#include <pic.h>
#include "UART.h"

/* void InitUART ( void ) */
/* { */
/* 	TRISB1 = 1;	//TXD and RXD pins must be set as inputs */
/* 	TRISB2 = 1;	 */
/* 	SPEN	= 1;		// UART enable */
/* 	BRGH = 1;		// if this bit is 0, then LOW SPEED */
/* 	RX9 = 0;		// 8Bit */
/* 	TXEN = 0;		// The Transmit is disable if TXEN == 0; */
/* 	CREN = 0;		// The Receive is disable if CREN == 0; */
/* 	SPBRG = 25;	//Bits Rate Transfer == 9600 */
	
/* } */

void putchar ( char ch )
{
	while(!TXIF)	/* set when register is empty */
		continue;
	TXREG = ch;
}

void puts ( const char *s )
{
	while ( *s )
	{
		putchar( *s++ );
	}	
}

void putsn ( unsigned char *s, unsigned char b_num )
{
	unsigned char i;
	for ( i = 0; i < b_num; i++ )
	{
		putchar(s[i]);
	}		
}


unsigned char UART_Receive_Buffer[UART_RECEIVE_BUFFER_SIZE];
unsigned char pUART_Receive_Buffer;
volatile bit UART_Receive_Complete;

/* UART receiver */

void UART_Receiver ( void )
{	
	unsigned char Rec_Byte = RCREG;

	UART_Receive_Buffer[pUART_Receive_Buffer++] = Rec_Byte;
	pUART_Receive_Buffer %= UART_RECEIVE_BUFFER_SIZE;

	if ( Rec_Byte == '\r' )
	{
		if ( pUART_Receive_Buffer )
		{
			UART_Receive_Buffer[pUART_Receive_Buffer] = 0;
			pUART_Receive_Buffer = 0;
			UART_Receive_Complete = 1;
		}
	}
}
